%%% Omar and Emily dataset
%
%% Load data
% Load text data into cell array
%fname='Breast-TMA_most_recent_with_edits_onlyValues_3-22-11.txt';
%fname='Breast-TMA_most_recent_with_edits_onlyValues_3-22-11_OS.txt';
fname='Breast-TMA_most_recent_with_edits_onlyValues_3-22-11_RFS.txt';
fid = fopen(fname,'r');
i=0;
while ~feof(fid)
    i=i+1;
    linha{i}=fgetl(fid);
end
fclose(fid);
%% Parse and normalize data
% Extract parameters, create dummy variable for categorical data
% and perform median replacement of missing values
%
% Generate Cell Matrix
n=length(linha)-1;
M={};
for i=1:n+1
    Mi=eval(['{''',strrep(linha{i},char(9),''','''),'''}']);
    M=[M;Mi];
end
%Get original parameter names
dt.parms=M(1,:);
%Find out the possibilities
m=length(M(1,:));
dt.num=[];
dt.numVar={};
dt.numVarCount=[];
varC=0;
for j=1:m
    xj=M(2:end,j);
    
    if isempty(regexp([xj{:}],'[^\d\.\,]')); %if only numeric characters show up
        dtNumi=[];
        for i=1:n;if ~isempty(xj{i});dtNumi=[dtNumi;str2num(strrep(xj{i},',',''))];end;end
        %dtNumi=str2num(cell2mat(xj));
        md=median(dtNumi);
        %look for missing values and replace them by median
        for i=1:n
            if isempty(xj{i})
                dtNumi(i)=md;
            else
                dtNumi(i)=str2num(strrep(xj{i},',',''));
            end
        end
        dt.num=[dt.num,dtNumi];
        %dtNumi=str2num(cell2mat(xj));
        varC=varC+1;
        dt.numVarCount=[dt.numVarCount;[j,varC,varC]];
        dt.numVar=[dt.numVar,dt.parms(j)];
    else
        % is it only numbers?
        4;
        % create dummy variables
        dumyVars=unique(xj);
        k=length(dumyVars);
        dtNumi=zeros(n,k);
        for i=1:k
            sf=strfind(xj,dumyVars{i});
            for ii=1:n
                dtNumi(ii,i)=~isempty(sf{ii});
            end
            dt.numVar=[dt.numVar,{[dt.parms{j},':',dumyVars{i}]}];
        end
        dt.num=[dt.num,dtNumi];
        varC=varC+k;
        dt.numVarCount=[dt.numVarCount;[j,varC-k,varC]];
        4;
    end
    
end

%% Create tab delimited data file for NRN

fid=fopen([fname(1:end-4),'_NRN',fname(end-3:end)],'w');
% print header
fprintf(fid,'%s',dt.numVar{1});
fprintf(fid,'\t%s',dt.numVar{2:end-1});
fprintf(fid,'\t%s\n',dt.numVar{end});
% print data
for i=1:n
    fprintf(fid,'%f',dt.num(i,1));
    fprintf(fid,'\t%f',dt.num(i,2:end-1));
    if i==n
        fprintf(fid,'\t%f',dt.num(i,end));
    else
        fprintf(fid,'\t%f\n',dt.num(i,end));
    end
end
fclose(fid);



